<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>新建分类</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="${request.contextPath}/admin_res/layui-admin/frame/layui/css/layui.css">
</head>
<body>
          
<form class="layui-form" action="return false;">

  <div class="layui-form-item">
    <label class="layui-form-label">分类名称</label>
    <div class="layui-input-block">
      <input name="categoryName" lay-verify="required" autocomplete="off" placeholder="请输入分类名称" class="layui-input" type="text">
    </div>
  </div>
  
  <div class="layui-form-item">
      <label class="layui-form-label">所属分类</label>
      <div class="layui-input-block">
        <input id="query_category" name="query_category" lay-verify="required" autocomplete="off" class="layui-input" >
      </div>
  </div>
  
  <div class="layui-form-item">
      <label class="layui-form-label">排序</label>
      <div class="layui-input-block">
        <input name="sortOrder" lay-verify="required|number" autocomplete="off" class="layui-input" type="number">
      </div>
  </div>
  
  
  <div class="layui-form-item">
    <label class="layui-form-label">是否启用</label>
    <div class="layui-input-block">
      <input checked="" name="status" lay-skin="switch" lay-text="ON|OFF" type="checkbox">
    </div>
  </div>
  
  <div class="layui-form-item">
    <div class="layui-input-block">
      <button class="layui-btn" lay-submit="" lay-filter="demo1">提交</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
</form>
 
<script type="text/javascript" src="${request.contextPath}/admin_res/layui-admin/frame/layui/layui.js"></script>
<script type="text/javascript" src="${request.contextPath}/admin_res/jquery-1.12.4.js"></script>
<script type="text/javascript" src="${request.contextPath}/admin_res/bootstrap.autocomplete.js"></script>
<script>
layui.use(['form', 'layedit'], function(){
  var form = layui.form
  ,layer = layui.layer
  ,layedit = layui.layedit ;
  
  query_category_autocomplete();
  
  //监听提交
  form.on('submit(demo1)', function(data){
	if(data.field.status=='on'){
		data.field.status=1 ;
	}else{
		data.field.status=0 ;
	}
	
	var category = $("#query_category").attr("real-value") ;
	if(category.indexOf("-")!=-1){
		var arr = category.split("-");
		data.field['fid'] = arr[0];//父ID
		data.field['categoryLevel'] = arr[1]*1+1;//层级
	}
	
    $.post("${request.contextPath}/admin/category/insertByEntity" , data.field , function(ret){
    	if(ret.status==200){
			  var index = parent.layer.getFrameIndex(window.name);
			  parent.layer.close(index);
			  parent.layer.msg("添加成功");
    	}else{
    		layer.msg(ret.msg);
    	}
    });
    return false;
  });
  
});

function query_category_autocomplete(){
	$("#query_category").autocomplete({
		items : 20 ,
        source:function(query,process){
        	$.post("${request.contextPath}/admin/category/getCategoryList" , {
        		categoryName : $("#query_category").val()
        	} , function(ret){
        		return process(ret.data);
        	})
        },
        formatItem:function(item){
        		return item["categoryName"] + "("+item["categoryLevel"]+"级)";
        },
        setValue:function(item){
        		return {'data-value':item["categoryName"],'real-value':item["id"]+"-"+item["categoryLevel"]};
        }
    });
	
}
</script>
</body>
</html>